package com.mall.dingdan.controller;


import com.mall.address.entity.Address;
import com.mall.address.service.AddressService;
import com.mall.dingdan.service.DingdanService;
import com.mall.commondataformat.R;
import com.mall.product.entity.Product;
import com.mall.product.service.ProductService;
import com.mall.user.entity.User;
import com.mall.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import static com.mall.commonutils.ResultCode.USER_NOT_LOGIN;
import static com.mall.commonutils.SessionStaticMes.USER_IN_SESSION;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-05-05
 */
@RestController
@RequestMapping("/order")
public class DingdanController {
/*
    @Autowired
    private DingdanService dingdanService;
    @Autowired
    private ProductService productService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    //查询商品状态
    @ApiOperation("查询商品状态")
    @PostMapping("/getStatus")
    public R getStatus(long ShangPin_id){
        com.mall.dingdan.entity.Dingdan dingdan = dingdanService.getById(ShangPin_id);
        return R.ok().data("isPay", dingdan.getIsPay()).data("isSend", dingdan.getIsSend()).data("isAccept", dingdan.getIsAccept());
    }

    //购买商品
    @ApiOperation("购买商品")
    @PostMapping("/buy")
    public R buy(long Product_id,int address_id,HttpSession session){

        User user = (User)session.getAttribute(USER_IN_SESSION);
        if (user == null){
            return R.error().message(USER_NOT_LOGIN);
        }

        com.mall.dingdan.entity.Dingdan dingdan =new com.mall.dingdan.entity.Dingdan();
        Address address=new Address();
        address=addressService.getById(address_id);
        Product product =new Product();
        product = productService.getById(Product_id);

        if(product.getPrice()<=user.getMoney()){
            user.setMoney(user.getMoney()- product.getPrice());
            product.setCount(product.getCount()-1);

            dingdan.setUserId(user.getId());
            dingdan.setShangpinId(product.getId());
            dingdan.setAccpetAddress(address.getContent());
            dingdan.setIsPay(1);
            dingdan.setIsSend(0);
            dingdan.setIsAccept(0);
            dingdan.setAccpetAddress(address.getContent());
            boolean res_dingdan = dingdanService.save(dingdan);

            boolean res_user=userService.updateById(user);
            boolean res_ShangPin= productService.updateById(product);
            if(res_ShangPin&&res_user&&res_dingdan){
                return R.ok();
            }
            else{
                return R.error().message("数据更新失败");
            }
        }
        else{
            return R.error().message("账户余额不足");
        }


    }*/
}

